import { defineStore } from "pinia";
import { computed, ref } from "vue";
import { selectTbGoodsByPageApi, lendGoodsSubListApi } from "@/api/modules/goods/index";
import { v4 as uuidv4 } from "uuid";
import { showLoadingToast, showToast } from "vant";
import { useRouter } from "vue-router";

const storeSetup = () => {
  // router
  const router = useRouter();

  // 当前查询的借还记录
  const currentLendReturnLogs = ref([]);

  // 当前查询的借还记录对应的货品id
  const currentLendReturnLogsGoodId = ref(null);

  // 借还记录加载标识
  const isLendReturnLogsLoading = ref(false);

  // 借还记录查询事件
  const lendGoodsSubListFunc = async () => {
    showLoadingToast({
      message: "加载中...",
      duration: 1000
      // forbidClick: true
    });
    isLendReturnLogsLoading.value = true;
    await lendGoodsSubListApi({
      tbGoodsId: currentLendReturnLogsGoodId.value,
      page: 1,
      size: 1000000
    })
      .then(res => {
        console.log(res);
        currentLendReturnLogs.value = res.data.list;
      })
      .finally(() => {
        isLendReturnLogsLoading.value = false;
        closeToast();
      });
  };

  // 当前要显示的借还记录
  // const currentShowLendReturnLogs = computed(() => {
  //   let rLogs = [];
  //   if (currentLendReturnType.value == "全部") {
  //     rLogs = currentLendReturnLogs.value;
  //   } else if (currentLendReturnType.value == "借出") {
  //     rLogs = currentLendReturnLogs.value.filter(item => {
  //       return item.makeName == "借出";
  //     });
  //   } else if (currentLendReturnType.value == "归还") {
  //     rLogs = currentLendReturnLogs.value.filter(item => {
  //       return item.makeName == "归还";
  //     });
  //   }
  //   return rLogs;
  // });

  // 借还记录当前分类
  const currentLendReturnType = ref("全部");

  // 当前进入选择货品的gid
  const currentSelectGoodGId = ref(null);

  // 该用户所有货品
  const allGoods = ref([]);

  // 货品列表加载标识
  const isGoodListLoading = ref(false);

  // 选择货品-搜索货品关键字
  const selectGoodSearchKeyword = ref("");

  // 选择货品点击事件
  const selectGoodClick = sGood => {
    console.log(sGood);
    lendGoodsData.value.forEach(good => {
      if (good.gid == currentSelectGoodGId.value) {
        good.id = sGood.id;
        good.name = sGood.name;
        good.quantity = sGood.quantity;
        good.lendQuantity = sGood.lendQuantity;
      }
    });
    selectGoodSearchKeyword.value = "";
    currentSelectGoodGId.value = null;
    router.back();
  };

  // 删除货品
  const delGood = good => {
    lendGoodsData.value = lendGoodsData.value.filter(item => {
      return item.gid != good.gid;
    });
  };

  // 选择展示的货品
  const showSelectGoods = computed(() => {
    let rGoods = [];
    let selectedGoodNames = [];
    lendGoodsData.value.forEach(item => {
      selectedGoodNames.push(item.name);
    });

    console.log("出发过滤", selectGoodSearchKeyword.value);
    if (
      selectGoodSearchKeyword.value == "" ||
      selectGoodSearchKeyword.value == undefined ||
      selectGoodSearchKeyword.value == null
    ) {
      rGoods = allGoods.value.filter(item => {
        return !selectedGoodNames.includes(item.name);
      });
    } else {
      rGoods = allGoods.value
        .filter(item => {
          return item.name.indexOf(selectGoodSearchKeyword.value) != -1;
        })
        .filter(item => {
          return !selectedGoodNames.includes(item.name);
        });
    }
    console.log(rGoods);
    return rGoods;
  });

  // 借出货品数据
  const lendGoodsData = ref([
    {
      gid: uuidv4(),
      id: null,
      name: "",
      makeQuantity: 0,
      quantity: 0,
      lendQuantity: 0,
      lendReturnDesc: ""
    }
  ]);

  // 添加货品点击事件
  const goodGoodClick = () => {
    // 添加前先判断上一个货品信息是否填写完整
    let lastGood = lendGoodsData.value[lendGoodsData.value.length - 1];

    if (lastGood.id != null && lastGood.makeQuantity != 0 && lastGood.makeQuantity != null) {
      lendGoodsData.value.push({
        gid: uuidv4(),
        id: null,
        name: "",
        makeQuantity: 0,
        quantity: 0,
        lendQuantity: 0,
        lendReturnDesc: ""
      });
    } else {
      if (lastGood.makeQuantity == 0) {
        showToast(`货品${lendGoodsData.value.length} 的数量不能为0`);
      } else {
        showToast(`请将 货品${lendGoodsData.value.length} 填写完整`);
      }
    }
  };

  // 查询所有货品的函数
  const selectTbGoodsByPageFunc = async () => {
    isGoodListLoading.value = true;
    await selectTbGoodsByPageApi({
      page: 1,
      size: 1000000
    })
      .then(res => {
        allGoods.value = res.data.list;
      })
      .finally(() => {
        isGoodListLoading.value = false;
      });
  };

  // 返回参数
  return {
    lendGoodsData,
    allGoods,
    selectGoodSearchKeyword,
    showSelectGoods,
    isGoodListLoading,
    currentSelectGoodGId,
    currentLendReturnLogs,
    currentLendReturnType,
    currentLendReturnLogsGoodId,
    isLendReturnLogsLoading,
    // currentShowLendReturnLogs,
    selectTbGoodsByPageFunc,
    goodGoodClick,
    selectGoodClick,
    delGood,
    lendGoodsSubListFunc
  };
};

// 导出Store
export const useLendGoodStore = defineStore("lendGood", storeSetup);
